import axios from 'axios'
import Fingerprint2 from 'fingerprintjs2'

class Request {
  static get(url) {
    return this.xhr('get', url)
  }

  static put(url, params) {
    return this.xhr('put', url, params)
  }

  static post(url, params) {
    return this.xhr('post', url, params)
  }

  static delete(url, params) {
    return this.xhr('delete', url, params)
  }

  static xhr(method, url, params) {
    return new Promise((resolve, reject) => {
      new Fingerprint2().get(function(finger, components){
        resolve(finger)
      })
    }).then( finger => {
      if(method === 'get') {
        url = `${url}&finger=${finger}`
      }
      let config = Object.assign({ method, url }, params ? { data: Object.assign(params, {finger}) } : { data: {finger} } )
      return axios(config)
    })
  }
}
export default Request